package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return menuDao.findMenuIdsByRoleId(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        String queryString = queryPageBean.getQueryString();
        Page<Menu> page = menuDao.findByCondition(queryString);
        long total = page.getTotal();
        List<Menu> result = page.getResult();
        return new PageResult(total, result);
    }

    @Override
    public List<Map> findMenu() {
        //根据菜单级别查找父菜单(一级菜单)
        List<Menu> parentMenus = menuDao.findMenuByLevel();
        List<Map> muneList = new ArrayList<>();

        for (Menu parentMenu : parentMenus) {
            Map<String, Object> menuMap = new HashMap<>();
            //根据父菜单id去查询子菜单集合
            Integer parentMenuId = parentMenu.getId();
            List<Menu> childrenMenus = menuDao.findByParentId(parentMenuId);
            List<Map> childrenList = new ArrayList<>();

            for (Menu childrenMenu : childrenMenus) {
                Map<String, Object> childrenMap = new HashMap<>();
                childrenMap.put("id", childrenMenu.getId());
                childrenMap.put("name", childrenMenu.getName());
                childrenMap.put("linkUrl", childrenMenu.getLinkUrl());
                childrenMap.put("path", childrenMenu.getPath());
                childrenMap.put("parentMenuId", childrenMenu.getParentMenuId());
                childrenMap.put("level", childrenMenu.getLevel());
                childrenList.add(childrenMap);
            }
            menuMap.put("id", parentMenu.getId());
            menuMap.put("name", parentMenu.getName());
            menuMap.put("linkUrl", parentMenu.getLinkUrl());
            menuMap.put("path", parentMenu.getPath());
            menuMap.put("parentMenuId", parentMenu.getParentMenuId());
            menuMap.put("level", parentMenu.getLevel());
            menuMap.put("children", childrenList);
            muneList.add(menuMap);
        }
        return muneList;
    }

    @Override
    public List<Menu> findAllMenu() {
        List<Menu> menus = menuDao.findAll();
        return menus;
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Menu findMenuById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void deleteByMenuId(Integer id) {
        Integer count = menuDao.findCountByMenuId(id);
        if (count>0){
            new RuntimeException();
        }
        menuDao.deleteByMenuId(id);
    }
}
